<?php
namespace Admin\Controller;
use Think\Controller;
class LinkController extends CommonController {


    public function index(){
        //查询所有 并发送
//        $this->links = $this->link->order('sort_order')->select();
        $Link = M('Link');
        $links = $Link->order('sort_order')->select();
        $this->assign("links",$links);
        $this->display();
    }
//    public function add(){
//        if(IS_POST){
//            $Link = M('Link');
//            $Link->create();
//            $Link->add();
//            $this->success("恭喜你，新增友情链接成功",U("index"));
//        }
//    }
    /***
     * ajax新增
     */
    public function ajax_add()
    {
        if (IS_AJAX) {
            $this->link->create();
            $id = $this->link->add();
            $data = ['id' => $id];
            $this->success($data);
        }
    }
//    public function update(){
//        $id = I('post.id');
//        if(IS_POST){
//            $Link = M('Link');
//            $Link->create();
//            $Link->where("id = '$id'")->save();
//            $this->success("恭喜你，编辑友情链接成功",U('index'));
//        }
//    }
    /***
     * ajax编辑
     */
    public function ajax_update()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->link->create();
            $this->link->where("id = '$id'")->save();
        }
    }
    public function delete()
    {
        $id = I('get.id');
        $this->link->delete($id);
        $this->success('恭喜你，删除友情链接成功', U('index'));
    }
    /***
     * ajax删除单条
     */
    public function delete_one()
    {
        $id = I('post.id');
        $this->link->delete($id);
    }


    /***
     * 多选删除
     */
    public function delete_all()
    {
        if (IS_POST) {
            $ids = I('post.ids');
            //第一种
//            $data['id'] = array('in', $ids);
//            $this->link->where($data)->delete();

            //第二种
            foreach ($ids as $id) {
                $this->link->where("id = '$id'")->delete();
            }

            $this->success('删除成功', U('index'));
        }
    }
    /***
     * ajax删除多条
     */
    public function del_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $v) {
                $this->link->where("id = '$v'")->delete();
            }
        }
    }


    /***
     * 排序
     */
    public function sort_order()
    {
        if (IS_POST) {
            $id = I('post.id');
            $sort_order = I('post.sort_order');

            foreach ($id as $k => $v) {
                $this->link->where("id = '$v'")->setField('sort_order', $sort_order[$k]);
            }
            $this->success('您已重新排序', U('index'));
        }
    }
}